import 'dart:async';
import 'package:fluwx/fluwx.dart';

class WechatHelper {
  ///微信登录授权
  Future<WeChatAuthResponse> weChatAuth() async {
    await sendWeChatAuth(
      scope: "snsapi_userinfo",
      state: "wechat_sdk_yaoditong",
    );
    var completer = Completer<WeChatAuthResponse>();
    StreamSubscription subscription;
    subscription = weChatResponseEventHandler.listen((resp) {
      subscription?.cancel();
      completer.complete(resp as WeChatAuthResponse);
    });
    return completer.future;
  }

  ///微信支付
  Future<WeChatPaymentResponse> weChatPay({
    appId,
    partnerId,
    prepayId,
    packageValue,
    nonceStr,
    timeStamp,
    sign,
  }) async {
    if (appId == null || partnerId == null || prepayId == null || packageValue == null || nonceStr == null || timeStamp == null || sign == null) {
      print('支付参数缺失');
      return null;
    }

    await payWithWeChat(appId: appId, partnerId: partnerId, prepayId: prepayId, packageValue: packageValue, nonceStr: nonceStr, timeStamp: timeStamp, sign: sign);
    var completer = Completer<WeChatPaymentResponse>();
    StreamSubscription subscription;
    subscription = weChatResponseEventHandler.listen((resp) {
      subscription?.cancel();
      completer.complete(resp as WeChatPaymentResponse);
    });
    return completer.future;
  }
}
